/*
 * Copyright (C) 2006-2015
 * 版权所有者为北京捷越联合信息咨询有限公司。本系统是商用软件,未经授权擅自复制或传播本程序的部分或全部将是非法的。
 * @title: VirtualCashModeFilter.java
 * @package com.jy.modules.cims.component.creditRight.plan
 * @author zhaox
 * @date 2015-3-5 下午4:39:37
 * @version v1.00
 * @description: TODO(用一句话描述该文件做什么)
 */

package com.jy.modules.cims.component.creditRight.plan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.data.dao.RepaymentPlanDao;
import com.jy.modules.cims.data.dto.credit.PlanChangeDTO;

/**
 * 未启用
 * 
 * @classname: VirtualCashModeFilter
 * @description: TODO(这里用一句话描述这个类的作用)
 */
@Service("com.jy.modules.cims.component.creditRight.plan.VirtualCashModeFilter")
public class VirtualCashModeFilter implements DealModeFilter {
    Logger logger = LoggerFactory.getLogger(VirtualCashModeFilter.class);
    @Autowired
    private RepaymentPlanDao repaymentPlanDao;

    /* (非 Javadoc)
    * <p>title: doFilter</p>
    * <p>description: </p>
    * @param object
    * @param dealModeChain
    * @see com.jy.modules.cims.component.creditRight.plan.DealModeFilter#doFilter(java.lang.Object, com.jy.modules.cims.component.creditRight.plan.DealModeChain)
    */

    @Override
    public void doFilter(int index,PlanChangeDTO planChangeDTO, DealModeChain dealModeChain) {
        // 通过合约判断是不是自己处理该类型
        String creditCode = planChangeDTO.getContractNo();
        String transferId = planChangeDTO.getTransferId();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("creditCode", creditCode);
        paramMap.put("transferId", transferId);
        // 通过合同号（债券编号）查询合约下约定的处理方式。没有查询到抛异常
        List<Map> list = repaymentPlanDao.queryPlanContractDealMode(paramMap);
        Map dealModeMap = new HashMap();
        for (Map map : list) {
            dealModeMap.put(map.get("TYPE"), map.get("DISPOSE_TYPE"));
        }
        // 还款计划变更类型
        String changeType = planChangeDTO.getChangeType();
        Object disposeType = dealModeMap.get(changeType);
        if (disposeType == null) {
            throw new CimsException("未配置该合同的违约信息！" + creditCode + "," + transferId);
        }
        if (CreditPlanConstant.DEAL_MODE_VIRUALCASH.equals(disposeType.toString())) {// 提前结清>虚拟现金流
            // 处理逻辑
            logger.info("处理类型为：虚拟现金流");
         // 判断当前处理日期，如果时间为1,16号。则把下一期更新为变更状态。
//            Calendar calendar = Calendar.getInstance();
//            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int term = planChangeDTO.getTerm();
//            if (day == 1 || day == 16) {
//                logger.info("当前变更日期为"+day+"号,变更状态更新到下一期！");
//                term = term + 1;
//            }
            paramMap.put("currentTime", term);
            repaymentPlanDao.updateVirtualRepaymentPlans(paramMap);// 更新虚拟现金流还款计划
            //repaymentPlanDao.updateVirtualRepaymentCosts(paramMap);
            paramMap.put("planStatus", changeType);
            repaymentPlanDao.updatePlanChanged(paramMap);// 更新该期还款计划的状态
            paramMap.put("creditStatus", changeType);
            //repaymentPlanDao.updateCostChanged(paramMap);//更新该期结算计划的状态 ,虚拟现金流按正常月结处理。
        } else {
            index++;
            dealModeChain.doFilter(index,planChangeDTO, dealModeChain);
        }
    }
}
